package crazymakercircle;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @Author: DRevelation
 * @Date: 2021/8/6 13:34
 */
public class JvmUtil {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Unsafe unsafe = getUnsafe();
        //非静态属性的偏移量
        long offset = unsafe.objectFieldOffset(null);
        unsafe.compareAndSwapObject("原始对象", offset, "期望值（旧值）", "更新值（新值）");
    }

    public static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
        Field theSafe = Unsafe.class.getDeclaredField("getUnsafe");
        theSafe.setAccessible(true);
        return (Unsafe) theSafe.get(null);
    }
}
